<?php
/**
 * 《帐号管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/JabberUtil.class.php');

class GroupUser extends JabberUtil {
	public $tbl = Constant::tbl_im_group_user;

	public $attribute = array(
		'-' => '请选择',
		'Cleartext-Password' => 'Cleartext-Password',
	);
	
	public $op = array(
		'-' => '请选择',
		':=' => ':=',
		'=' => '=',
		
		'>' => '>',
		'<' => '<',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '正常',
		'2' => '暂停',
		'3' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'info' => '分类',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('attribute_option', $this->attribute);
		$controller->assign_by_ref('op_option', $this->op);
	}

	function tab($controller, $request) {
		$data = array(
			'帐号管理' => array('cc'=>'jabber.php?do=User.find', 'js'=>'1'),
			'好友信息' => array('cc'=>'jabber.php?do=Roster.find', 'js'=>'1'),
			'好友分组' => array('cc'=>'jabber.php?do=RosterGroup.find', 'js'=>''),
			'分组信息' => array('cc'=>'jabber.php?do=Group.find', 'js'=>''),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'jabber/group/user/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}
		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($this->_audit('111')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _where($request) {
		$name_s = $request['name_s'];
		
		$where = " WHERE 1=1";
		
		if (!empty($name_s)) {
			$where .= sprintf(" AND (name LIKE '%s' OR username LIKE '%s')", $name_s . '%', $name_s . '%');
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby('groupName');
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'jabber/group/user/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'radius/check/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$username = $request['username'];
		$attribute = $request['attribute'];
		$op = $request['op'];
		$value = $request['value'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(username, attribute, op, value, content, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $username, $attribute, $op, $value, $content);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'jabber/group/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$name = $request['name'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET groupName = '%s', description = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'radius/check/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'radius/check/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
	
		$row = $this->_view_t($row);
	
		return $row;
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		echo substr("0755-89809672", 5);

		logdate($controller, $request);
		return true;
	}
	
	function _update_groupname($from, $to) {
		if ($from == $to) {
			return false;
		}
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET groupName = '%s' WHERE groupName = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $from);
		$db->query($sql);
	}
}
?>